//
//  ControlRegistry.m
//  iCodeMac
//
//  Created by Andrew Barry on 16/05/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ControlRegistry.h"

static ControlRegistry *gSharedInstance;

@implementation ControlRegistry
+(ControlRegistry *) instance {
	if (!gSharedInstance)
		gSharedInstance = [ControlRegistry new];
	return gSharedInstance;
}

-(void)addControl: (NSString *) name withBase: (NSString *) basename withFactory: (NSString *)factoryClass {
	[names addObject:name];
	[basenames addObject:basename];
	[factories addObject:factoryClass];
}

-(id) init {
	self = [super init];
	
	names = [NSMutableArray new];
	basenames = [NSMutableArray new];
	factories = [NSMutableArray new];
	
	[self addControl:@"Button" withBase:@"button" withFactory:@"ButtonFormControl"];
	[self addControl:@"Label" withBase:@"label" withFactory:@"TextFieldFormControl"];
	[self addControl:@"Textfield" withBase:@"textfield" withFactory:@"TextFieldFormControl"];
	[self addControl:@"Checkbox" withBase:@"checkbox" withFactory:@"ButtonFormControl"];
	[self addControl:@"Radiobutton" withBase:@"radio" withFactory:@"ButtonFormControl"];
	[self addControl:@"Tableview" withBase:@"tableview" withFactory:@"TableViewControl"];
	[self addControl:@"Outlineview" withBase:@"outlineview" withFactory:@"TableViewControl"];
	[self addControl:@"BrowserView" withBase:@"browserview" withFactory:@"BrowserViewControl"];
	[self addControl:@"Canvas" withBase:@"canvas" withFactory:@"CanvasControl"];
	[self addControl:@"Timer" withBase:@"timer" withFactory:@"TimerControl"];
	[self addControl:@"Box" withBase:@"box" withFactory:@"BoxControl"];
	[self addControl:@"Progress" withBase:@"progress" withFactory:@"ProgressControl"];
	[self addControl:@"Spinner" withBase:@"spinner" withFactory:@"ProgressControl"];
	[self addControl:@"Popup" withBase:@"popup" withFactory:@"PopupControl"];
	[self addControl:@"Web" withBase:@"web" withFactory:@"WebControl"];
	[self addControl:@"Data" withBase:@"data" withFactory:@"DataControl"];

	return self;
}

-(int)count {
	return [names count];
}

-(NSString *) nameForIndex: (int) idx {
	return [names objectAtIndex:idx];
}

-(NSString *) basenameForIndex: (int) idx {
	return [basenames objectAtIndex:idx];
}

-(FormControl *) controlForName: (NSString *) name {
	int idx = [basenames indexOfObject:name];
	if (idx < 0)
		return nil;
	NSString *className = [factories objectAtIndex:idx];
	Class factoryClass = NSClassFromString(className);
	return [factoryClass new];
}
	 
@end
